home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / csh4.zip / PUSHD.C < prev    next >
C/C++ Source or Header  |  1985-09-04  |  1KB  |  61 lines

  1. #define NULL (void *)0
  2.  
  3. static char *dirstack[10];
  4. static int dsp = -1;
  5.  
  6. pushd(argc,argv)
  7. char *argv[];
  8. {
  9.     char *getcwd(), *savestr();
  10.     static char *usage = "usage : pushd newdir";
  11.     static char *pusherr = "pushd : dir stack overflow";
  12.     char dirbuf[64];
  13.     if (argc == 1)
  14.     {
  15.         write(2,usage,strlen(usage));
  16.         crlf();
  17.         return -1;
  18.     }
  19.     if (NULL ==  getcwd(&dirbuf[1],64))
  20.     {
  21.         perror("pushd");
  22.         return -1;
  23.     }
  24.     if (++dsp == 10)
  25.     {
  26.         write(2,pusherr,strlen(pusherr));
  27.         crlf();
  28.         return -1;
  29.     }
  30.     dirbuf[0] = '\\';
  31.     if (-1 == chdir(*(++argv)))
  32.     {
  33.         --dsp;
  34.         perror("pushd");
  35.         return -1;
  36.     }
  37.     dirstack[dsp] = savestr(dirbuf);
  38.     return 0;
  39. }
  40.  
  41. popd()
  42. {
  43.     register int returnval = 0;
  44.     static char *poperr = "popd : dir stack underflow";
  45.     if (dsp == -1)
  46.     {
  47.         write(2,poperr,strlen(poperr));
  48.         crlf();
  49.         return -1;
  50.     }
  51.     if (-1 == chdir(dirstack[dsp]))
  52.     {
  53.         perror("popd");
  54.         write(2,dirstack[dsp],strlen(dirstack[dsp]));
  55.         crlf();
  56.         returnval = -1;
  57.     }
  58.     free(dirstack[dsp--]);
  59.     return returnval;
  60. }
  61.